﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCenter.DataProvider;
using UCenter.Models;
using UCenter.Core.Repository;
using System.Data.Linq;
using System.Linq.Dynamic;

namespace UCenter.SQLServerProvider
{
    /// <summary>
    /// FriendRepository
    /// </summary>
    public class FriendRepository : SqlRepositoryBase<Friendship>, IFriendRepository
    {
        #region Query Methods  
        public bool Exist(int userID, int friendID)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var f = from fd in db.GetTable<Friendship>()
                        where fd.UserId == userID & fd.FriendId == friendID
                        select fd;
                var friend = f as Friendship;
                return friend != null;
            }
        }

        public List<Friendship> GetFriends(int userID, string kw)
        {
            if (kw == null)
            {
                kw = "";
            }
            using (RepositoryContext db = new RepositoryContext())
            {
                DataLoadOptions option = new DataLoadOptions();
                option.LoadWith<Friendship>(f => f.FriendUser);
                option.LoadWith<User>(u => u.Profile);
                db.LoadOptions = option;
                var friends = (from f in db.GetTable<Friendship>()
                               where f.UserId == userID && f.User.Profile.TrueName.Contains(kw)
                               select f);
                return friends.ToList<Friendship>();
            }
        }

        #endregion

        #region Insert Methods      
        #endregion

        #region Delete Methods
        public void Delete(int userID, int friendID)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var f = from fd in db.GetTable<Friendship>()
                        where (fd.UserId == userID & fd.FriendId == friendID) || (fd.UserId == friendID & fd.FriendId == userID)
                        select fd;                
                 db.GetTable<Friendship>().DeleteAllOnSubmit(f);
                 db.SubmitChanges();
            }
        }
        #endregion

        #region Update Methods
        public void UpdateGroupSetting(int userID,int friendID, int newGroupID)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var f = (from fd in db.GetTable<Friendship>()
                        where fd.UserId == userID && fd.FriendId==friendID
                        select fd).SingleOrDefault<Friendship>();
                f.GroupId = newGroupID;
                db.SubmitChanges();
            }
        }
        #endregion
        
        public void Through(int userID, int friendID,int groupID,string note) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = (from f in db.GetTable<Friendship>()
                         where f.UserId == userID && f.FriendId == friendID
                         select f).SingleOrDefault<Friendship>();
                if (q != null) {
                    q.ConfirmDate = DateTime.Now;
                    q.IsConfirmed = true;
                    q.Note = note;
                }
                Friendship nf = new Friendship();
                nf.Note = note;
                nf.ConfirmDate = DateTime.Now;
                nf.CreateDate = DateTime.Now;
                nf.FriendId = userID;
                nf.GroupId = groupID;
                nf.IsConfirmed = true;
                nf.UserId = friendID;
                db.GetTable<Friendship>().InsertOnSubmit(nf);
                db.SubmitChanges();
            }
        }
    }
}
